package com.example.test.database;

import static com.example.test.database.DatabaseHelper.COLUMN_AGE;
import static com.example.test.database.DatabaseHelper.COLUMN_CONCERN;
import static com.example.test.database.DatabaseHelper.COLUMN_LOCAL;
import static com.example.test.database.DatabaseHelper.COLUMN_NAME;
import static com.example.test.database.DatabaseHelper.COLUMN_PASSWORD;
import static com.example.test.database.DatabaseHelper.COLUMN_PAY_PASSWORD;
import static com.example.test.database.DatabaseHelper.COLUMN_PHONE;
import static com.example.test.database.DatabaseHelper.TABLE_USERS;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.test.entity.News;
import com.example.test.entity.User;

public class UserDAO {
    private DatabaseHelper databaseHelper;
    public NewsDBHelper newsDBHelper ;
    public UserDAO(Context context) {
        databaseHelper = new DatabaseHelper(context);
        newsDBHelper = new NewsDBHelper(context);
    }

    public boolean isUserExists(String phone) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String[] columns = { COLUMN_PHONE };
        String selection = COLUMN_PHONE + " = ?";
        String[] selectionArgs = { phone };
        Cursor cursor = db.query(DatabaseHelper.TABLE_USERS, columns, selection, selectionArgs, null, null, null);
        boolean exists = cursor.getCount() > 0;
        cursor.close();
        db.close();
        return exists;
    }

    public void addUser(String phone, String password) {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_PHONE, phone);
        values.put(COLUMN_PASSWORD, password); // Assume other fields are null or empty
        values.put(COLUMN_PAY_PASSWORD , "000000");
        db.insert(DatabaseHelper.TABLE_USERS, null, values);
        db.close();
    }

    @SuppressLint("Range")
    public User getUser(String phone){
        SQLiteDatabase db = databaseHelper.getReadableDatabase() ;
        Cursor cursor = db.query("user" , new String[]{COLUMN_NAME , COLUMN_PASSWORD ,DatabaseHelper.COLUMN_CONCERN, COLUMN_AGE,
        COLUMN_PAY_PASSWORD , COLUMN_LOCAL} , COLUMN_PHONE + " = ? " ,
                new String[]{phone} , null , null , null , null);
        User user = null ;
        if (cursor != null && cursor.moveToFirst()){
            user = new User(
                    cursor.getString(cursor.getColumnIndex(COLUMN_NAME)),
                    cursor.getString(cursor.getColumnIndex(COLUMN_PASSWORD)),
                    cursor.getString(cursor.getColumnIndex(COLUMN_CONCERN)),
                    cursor.getString(cursor.getColumnIndex(COLUMN_AGE)),
                    cursor.getString(cursor.getColumnIndex(COLUMN_PAY_PASSWORD)),
                    cursor.getString(cursor.getColumnIndex(COLUMN_LOCAL))
            );
            cursor.close();
        }
        return user ;
    }

    public Cursor getAllUsers() {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String[] columns = {
                COLUMN_PHONE,
                COLUMN_NAME,
                COLUMN_PASSWORD,
                DatabaseHelper.COLUMN_CONCERN,
                COLUMN_AGE,
                COLUMN_PAY_PASSWORD,
                COLUMN_LOCAL
        };
        return db.query(DatabaseHelper.TABLE_USERS, columns, null, null, null, null, null);
    }

    /**
     * 验证提供的密码是否与数据库中存储的密码匹配。
     *
     * @param phone     用户的电话号码
     * @param password  提供的密码
     * @return 如果密码正确返回true，否则返回false
     */
    public boolean isValidPassword(String phone, String password) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String[] columns = { COLUMN_PASSWORD };
        String selection = COLUMN_PHONE + " = ?";
        String[] selectionArgs = { phone };
        String storedPassword = null;
        boolean isValid = false;

        Cursor cursor = null;
        try {
            cursor = db.query(DatabaseHelper.TABLE_USERS, columns, selection, selectionArgs, null, null, null);
            if (cursor.moveToFirst()) {
                // 假设密码以明文存储，实际应用中应该使用散列值比较
                int passwordIndex = cursor.getColumnIndex(COLUMN_PASSWORD);
                storedPassword = cursor.getString(passwordIndex);
                isValid = password.equals(storedPassword);
            }
        } catch (Exception e) {
            // 处理可能的异常，例如数据库查询失败
            e.printStackTrace();
        } finally {
            // 确保游标被关闭
            if (cursor != null) {
                cursor.close();
            }
            // 关闭数据库
            db.close();
        }

        return isValid;
    }


    public Cursor getUserByPhone(String phone) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String query = "SELECT * FROM " + TABLE_USERS + " WHERE " + COLUMN_PHONE + " = ?";
        return db.rawQuery(query, new String[]{phone});
    }

    //修改密码
    public void updatePasswordByPhone(String phone , String newPassword) {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_PASSWORD, newPassword);
        String selection = COLUMN_PHONE + " = ?";
        db.update(TABLE_USERS, values, selection, new String[]{phone});
    }
    // 修改支付密码
    public void updatePayPasswordByPhone(String phone, String newPayPassword) {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_PAY_PASSWORD, newPayPassword);
        String selection = COLUMN_PHONE + " = ?";
        db.update(TABLE_USERS, values, selection, new String[]{phone});
    }

    //修改年龄
    public void updateAgeByPhone(String phone, String newAge) {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_AGE, newAge);
        String selection = COLUMN_PHONE + " = ?";
        db.update(TABLE_USERS, values, selection, new String[]{phone});
    }

//修改姓名
    public void updateNameByPhone(String phone , String newName){
        SQLiteDatabase db = databaseHelper.getWritableDatabase() ;
        ContentValues values = new ContentValues() ;
        values.put(COLUMN_NAME , newName);
        String selection = COLUMN_PHONE + " = ? ";
        db.update(TABLE_USERS , values ,selection ,new String[]{phone}) ;
    }
    //修改地址
    public void updateLocalByPhone(String phone, String newLocal) {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_LOCAL, newLocal);
        String selection = COLUMN_PHONE + " = ?";
        db.update(TABLE_USERS, values, selection, new String[]{phone});
    }
    public void updateConcernByPhone(String phone , String newAvatar){
        SQLiteDatabase db = databaseHelper.getWritableDatabase() ;
        ContentValues values = new ContentValues() ;
        values.put(COLUMN_CONCERN , newAvatar);
        String selection = COLUMN_PHONE + " = ?";
        db.update(TABLE_USERS , values ,selection ,new String[]{phone}) ;
    }





    //以下是关于农技学院新闻表的操作方法
    //添加文章
    public boolean addNews(String news_type , String first_title , String second_title , String imgPath , String inputText , String publisher){
        SQLiteDatabase db = newsDBHelper.getWritableDatabase() ;
        ContentValues values = new ContentValues() ;
        values.put(NewsDBHelper.NEWS_TYPE , news_type);
        values.put(NewsDBHelper.NEWS_TITLE , first_title);
        values.put(NewsDBHelper.NEWS_SECOND_TITLE , second_title);
        values.put(NewsDBHelper.NEWS_IMG , imgPath);
        values.put(NewsDBHelper.NEWS_TEXT , inputText);
        values.put(NewsDBHelper.NEWS_PUBLISHER , publisher);
        long isAdd = db.insert(NewsDBHelper.TABLE_NEWS , null , values) ;
        return isAdd != -1;
    }
}
